﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Metadata;

namespace OptionSetBatchManager
{
    public static class ExtensionMethods
    {
        public static string DisplayText(this OptionSetMetadataBase optionSetMetadata)
        {
            if (optionSetMetadata.DisplayName != null && optionSetMetadata.DisplayName.UserLocalizedLabel != null && !string.IsNullOrEmpty(optionSetMetadata.DisplayName.UserLocalizedLabel.Label))
            {
                return string.Format("{0} ({1})", optionSetMetadata.DisplayName.UserLocalizedLabel.Label, optionSetMetadata.Name);
            }
            else
            {
                return optionSetMetadata.Name;
            }
        }

        public static string DisplayText(this AttributeMetadata attributeMetadata)
        {
            if (attributeMetadata.DisplayName != null && attributeMetadata.DisplayName.UserLocalizedLabel != null && !string.IsNullOrEmpty(attributeMetadata.DisplayName.UserLocalizedLabel.Label))
            {
                return string.Format("{0} ({1})", attributeMetadata.DisplayName.UserLocalizedLabel.Label, attributeMetadata.LogicalName);
            }
            else
            {
                return attributeMetadata.LogicalName;
            }
        }

        public static string DisplayText(this EntityMetadata entityMetadata)
        {
            if (entityMetadata.DisplayName != null && entityMetadata.DisplayName.UserLocalizedLabel != null && !string.IsNullOrEmpty(entityMetadata.DisplayName.UserLocalizedLabel.Label))
            {
                return string.Format("{0} ({1})", entityMetadata.DisplayName.UserLocalizedLabel.Label, entityMetadata.LogicalName);
            }
            else
            {
                return entityMetadata.LogicalName;
            }
        }
    }
}
